﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using dw = System.Drawing;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using wf = System.Windows.Forms;

namespace client.app.world
{
    public class UserInput : comm.world.BObject, IBObject
    {
        public const int GUI_THREAD_SLEEP = 1;// 1000 / 60;

        Texture2D _texLineH = null;
        Texture2D _texLineV = null;
        Rectangle _selectRect;
        bool _select = false;
        int[] _start = null;

        SpriteBatch _spriteBatch = null;

        public UserInput()
        {
            _spriteBatch = new SpriteBatch(MainGame.Instance.GraphicsDevice);

            var lineColor = dw.Color.White;
            using (var bitmap = new dw.Bitmap(3000, 1))
            {
                bitmap.Draw(g => { g.Clear(lineColor); });
                bitmap.Save(ms => { _texLineH = Texture2D.FromStream(MainGame.Instance.GraphicsDevice, ms); });
            }
            using (var bitmap = new dw.Bitmap(1, 3000))
            {
                bitmap.Draw(g => { g.Clear(lineColor); });
                bitmap.Save(ms => { _texLineV = Texture2D.FromStream(MainGame.Instance.GraphicsDevice, ms); });
            }

            var form = wf.Form.FromHandle(MainGame.Instance.Window.Handle);
            form.MouseClick += new wf.MouseEventHandler(form_MouseClick);
            form.MouseDown += new wf.MouseEventHandler(form_MouseDown);
            form.MouseMove += new wf.MouseEventHandler(form_MouseMove);
            form.MouseUp += new wf.MouseEventHandler(form_MouseUp);
            form.MouseDoubleClick += new wf.MouseEventHandler(form_MouseDoubleClick);
        }

        void form_MouseDoubleClick(object sender, wf.MouseEventArgs e)
        {
            if (!MainGame.Instance.IsActive)
            {
                return;
            }
            MainGame.MainWindow.CallMouseEvent(gui2.MouseEventType.DOUBLE_CLICK, e.X, e.Y);
        }
        void form_MouseDown(object sender, wf.MouseEventArgs e)
        {
            if (!MainGame.Instance.IsActive)
            {
                return;
            }
            _select = true;
            _start = new int[] { e.X, e.Y };

            MainGame.MainWindow.CallMouseEvent(e.Button == wf.MouseButtons.Left ? gui2.MouseEventType.LEFT_DOWN : gui2.MouseEventType.RIGHT_DOWN, e.X, e.Y);
        }
        void form_MouseUp(object sender, wf.MouseEventArgs e)
        {
            if (!MainGame.Instance.IsActive)
            {
                return;
            }
            if (_select)
            {
                MainGame.MainSpace.SelectSObj(_selectRect);
                _select = false;
                _selectRect = new Rectangle();
            }
            MainGame.MainWindow.CallMouseEvent(e.Button == wf.MouseButtons.Left ? gui2.MouseEventType.LEFT_UP : gui2.MouseEventType.RIGHT_UP, e.X, e.Y);
        }
        void form_MouseMove(object sender, wf.MouseEventArgs e)
        {
            if (!MainGame.Instance.IsActive)
            {
                return;
            }
            if (_select)
            {
                int sx = _start[0], sy = _start[1], ex = e.X, ey = e.Y;
                int x = sx, y = sy, w = ex - sx, h = ey - sy;
                if (sx > ex)
                {
                    x = ex;
                    w = sx - ex;
                }
                if (sy > ey)
                {
                    y = ey;
                    h = sy - ey;
                }
                _selectRect = new Rectangle(x, y, w, h);
            }

            MainGame.MainWindow.CallMouseEvent(gui2.MouseEventType.MOVE, e.X, e.Y);
        }
        void form_MouseClick(object sender, wf.MouseEventArgs e)
        {
            if (!MainGame.Instance.IsActive)
            {
                return;
            }
            var stop = MainGame.MainWindow.CallMouseEvent(gui2.MouseEventType.CLICK, e.X, e.Y);
            if (!stop)
            {
                if (e.Button == wf.MouseButtons.Right)
                {
                    MainGame.MainSpace.MouseRightClick(e.X, e.Y);
                }
            }
        }

        public void BeginDraw(comm.TimeStat timeStat)
        {
        }
        public void Draw(comm.TimeStat timeStat)
        {
            _spriteBatch.Begin();
            if (_select && _selectRect.Width > 1 && _selectRect.Height > 1)
            {
                var r = _selectRect;
                //top
                _spriteBatch.Draw(_texLineH, new Rectangle(r.Left, r.Top, r.Width, 1), Color.White);
                //bottom
                _spriteBatch.Draw(_texLineH, new Rectangle(r.Left, r.Bottom, r.Width, 1), Color.White);
                //left
                _spriteBatch.Draw(_texLineH, new Rectangle(r.Left, r.Top, 1, r.Height), Color.White);
                //right
                _spriteBatch.Draw(_texLineH, new Rectangle(r.Right, r.Top, 1, r.Height + 1), Color.White);
            }
            _spriteBatch.End();
        }

        protected override void OnUpdate(comm.TimeStat timeStat)
        {
            var keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Escape))
            {
                MainGame.Instance.Exit();
            }
            else if (keyState.IsKeyDown(Keys.Up))
            {
                MainGame.MainSpace.MoveView("up");
            }
            else if (keyState.IsKeyDown(Keys.Left))
            {
                MainGame.MainSpace.MoveView("left");
            }
            else if (keyState.IsKeyDown(Keys.Right))
            {
                MainGame.MainSpace.MoveView("right");
            }
            else if (keyState.IsKeyDown(Keys.Down))
            {
                MainGame.MainSpace.MoveView("down");
            }
        }
    }
}
